package com.salinity.kun.helper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.salinity.kun.algorithm.harmonic.ConstantParamHelper;
import com.salinity.kun.algorithm.harmonic.InitialPhase;
import com.salinity.kun.algorithm.harmonic.InterFactorAndCorAngle;
import com.salinity.kun.algorithm.harmonic.TideSymbol;
import com.salinity.kun.util.DateUtil;

public class PredictionHelper {

	private Date startDate;
	private double S0;
	private double[] h;
	private double[] g;
	private List<TideSymbol> tideSymbolList;
	private InitialPhase initialPhase = new  InitialPhase();
	public PredictionHelper(Date startDate, List<TideSymbol> tideSymbolList, double S0, double[] h, double[] g) {
		setStartDate(startDate);
		setS0(S0);
		setH(h);
		setG(g);
		setTideSymbolList(tideSymbolList);
	}

/**
 * 
 * @param predictionDate
 * @return
 * @throws ParseException 
 */
	public double doPrediction(Date  predictionDate) throws ParseException {
		double rslt = 0.0,f,fAndU[],sigma,t,V0,u;
		
		rslt+=S0;
		int n = tideSymbolList.size();

		InterFactorAndCorAngle IFACAngle = new InterFactorAndCorAngle(predictionDate);
	
		initialPhase.setTime(startDate);
		t=(DateUtil.Date2Calendar(predictionDate).getTimeInMillis()-DateUtil.Date2Calendar(startDate).getTimeInMillis())/(1000*60);
		t/=60f; //转化为小时
		for(int i=0;i<n;i++) {
			fAndU = IFACAngle.getValue(tideSymbolList.get(i));
			initialPhase.setTideSymbol(tideSymbolList.get(i));
			f=fAndU[0];
			u=fAndU[1];
			V0 = initialPhase.getV0()%360f;
			sigma = ConstantParamHelper.getAngularVelocity().getAngel(tideSymbolList.get(i));

			rslt+=f*h[i]*Math.cos((sigma*t+V0+u-g[i])*Math.PI/180);
		}
		
		
		return rslt;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public double[] getH() {
		return h;
	}

	public void setH(double[] h) {
		this.h = h;
	}

	public double[] getG() {
		return g;
	}

	public void setG(double[] g) {
		this.g = g;
	}

	public double getS0() {
		return S0;
	}

	public void setS0(double s0) {
		S0 = s0;
	}

	public List<TideSymbol> getTideSymbolList() {
		return tideSymbolList;
	}

	public void setTideSymbolList(List<TideSymbol> tideSymbolList) {
		this.tideSymbolList = tideSymbolList;
	}
}
